package com.yun.zf.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.yun.zf.app.service.SysDocumentsService;
import com.yun.zf.app.service.ZfCommunityService;
import com.yun.zf.app.service.ZfHouseBasicService;
import com.yun.zf.common.admin.domain.SysDocuments;
import com.yun.zf.common.admin.model.house.SearchMo;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.app.dto.detail.CommunityNearByDto;
import com.yun.zf.common.app.dto.detail.HouseDetailInfoDto;
import com.yun.zf.common.app.dto.home.OpenCityListDto;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.service.ZfCityService;
import com.yun.zf.system.service.ZfSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "首页")
@RequestMapping("/home")
@Slf4j
public class HomeController extends BasicController {
    @Autowired
    private ZfCommunityService communityService;

    @Autowired
    private ZfCityService cityService;
    @Autowired
    private SysDocumentsService documentsService;
    @Autowired
    private ZfSearchService searchService;
    @Autowired
    private ZfHouseBasicService houseBasicService;

    @PostMapping("/getOpenCity")
    @ApiOperation(value = "获取开通城市列表")
    public Response<OpenCityListDto> getOpenCity(){
        OpenCityListDto openCityListDto = new OpenCityListDto();
        openCityListDto.setHotCity(cityService.getHotCity());
        openCityListDto.setOpenList( cityService.getOpenCity());
        return  Response.success(openCityListDto);
    }
    @PostMapping("/getHomeBanner")
    @ApiOperation(value = "首页banner列表")
    @DynamicParameters(properties = {
            @DynamicParameter(name = "themeid",value = "页面",example = "111",required = true)
    })
    public Response<Map<String , List<SysDocuments>>> getHomeBanner(@RequestBody Map<String,String> map){
        return  Response.success(documentsService.getDocumentsByTheme(map.get("themeid"),1000));
    }

    @PostMapping("/brandCommunity")
    @ApiOperation(value = "品质小区推荐")
    @LogPrint
    @DynamicParameters(properties = {
            @DynamicParameter(name = "current",value = "当前页",example = "1",required = true),
            @DynamicParameter(name = "pageSize",value = "页大小",example = "10",required = true)
    })
    public Response<Page<CommunityNearByDto>> brandCommunity(@RequestBody Map<String,String> map) {
        int current=Integer.parseInt(map.get("current"));
        int pageSize=Integer.parseInt(map.get("pageSize"));
        return Response.success(communityService.getBrandCommunity(aCode,current,pageSize));
    }


    @PostMapping("/appSearchCondition")
    @ApiOperation(value = "搜索条件")
    @DynamicParameters(properties = {
            @DynamicParameter(name = "type",value = "类型",example = "1",required = true),
            @DynamicParameter(name = "cat",value = "分类",example = "area",required = true)
    })
    public Response<SearchMo> appSearchCondition(@RequestBody  Map<String,String> map){
        SearchMo searchMo = searchService.getSearchMo(map.get("type"),aCode,map.get("cat"));
        return  Response.success(searchMo);
    }


    @ApiOperation(value = "房源详情")
    @PostMapping("/getHouseDetailInfo")
    @DynamicParameters(properties = {
            @DynamicParameter(name = "houseId",value = "房源编号",example = "1",required = true)
    })
    public Response<HouseDetailInfoDto> getHouseDetailInfo(@RequestBody Map<String, String> map) {
        return Response.success(houseBasicService.getHouseDetailInfo(prov, aCode, Long.parseLong(map.get("houseId"))));
    }

    @ApiOperation(value = "异步获取同小区房源和周边小区数据")
    @PostMapping("/getHouseAndC")
    @DynamicParameters(properties = {
            @DynamicParameter(name = "houseId",value = "房源编号",example = "1",required = true),
            @DynamicParameter(name = "communityId",value = "小区编号",example = "1",required = true),
            @DynamicParameter(name = "type",value = "房源类型",example = "1",required = true)
    })
    public Response<HouseDetailInfoDto> getHouseAndC(@RequestBody Map<String, String> map) {
        String houseId = map.get("houseId");
        String communityId =map.get("communityId");
        String type =map.get("type");
        if (StringUtils.isBlank(houseId)||StringUtils.isBlank(communityId)||StringUtils.isBlank(type)){
            return Response.error("缺少必要参数");
        }
        return Response.success(houseBasicService.getHouseAndC(prov, aCode, Long.parseLong(houseId),Long.parseLong(communityId),Integer.parseInt(type)));
    }


}
